home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic 4 Database How-To / Visual Basic 4 Database - How-to (The Waite Group)(1995).iso / author.cl_ / author.cl
Text File  |  1995-03-12  |  4KB  |  150 lines

  1. Version 1.0 Class
  2. Attribute VB_Name = "clsAuthor"
  3. Attribute VB_Creatable = True
  4. Attribute VB_Exposed = True
  5. Option Explicit
  6.  
  7. Private m_Name As String
  8. Private m_Born As Integer
  9. Private m_ID As Long
  10. Private m_Database As String
  11. Private m_Titles As New Collection
  12.  
  13. Property Let AuthorName(theName As String)
  14.     m_Name = theName
  15. End Property
  16. Property Let YearBorn(theYear As Integer)
  17.     If theYear <= Year(Now()) And theYear >= 1 Then m_Born = theYear
  18. End Property
  19. Property Let DatabaseName(theDatabase As String)
  20.     m_Database = theDatabase
  21. End Property
  22. Property Get AuthorName() As String
  23.     AuthorName = m_Name
  24. End Property
  25. Property Get YearBorn() As Integer
  26.     YearBorn = m_Born
  27. End Property
  28. Property Get AuthorID() As Long
  29.     AuthorID = m_ID
  30. End Property
  31. Property Get DatabaseName() As String
  32.     DatabaseName = m_Database
  33. End Property
  34. Property Get Titles() As Collection
  35.     Set Titles = m_Titles
  36. End Property
  37. Public Sub Initialize(rs As Recordset)
  38.     m_ID = rs![Au_ID]
  39.     If Not IsNull(rs![author]) Then m_Name = rs![author] Else m_Name = ""
  40.     If Not IsNull(rs![Year Born]) Then m_Born = rs![Year Born] Else m_Born = 0
  41. End Sub
  42. Public Function NumberOfTitles(readNow As Boolean) As Integer
  43.     If readNow Then ReadTitles
  44.     NumberOfTitles = m_Titles.Count
  45. End Function
  46. Public Function GetTitle(tl As clsTitle) As Boolean
  47.     Dim i As Integer
  48.     Dim found As Boolean
  49.     
  50.     found = False
  51.     If m_Titles.Count = 0 Then ReadTitles
  52.     For i = 1 To m_Titles.Count
  53.         If m_Titles(i).Title = tl.Title Then
  54.             Set tl = m_Titles(i)
  55.             found = True
  56.             Exit For
  57.         End If
  58.     Next
  59.     GetTitle = True
  60. End Function
  61. Public Function ReadAuthor(whichAuthor As Variant, Optional theDatabaseName As Variant) As Boolean
  62.     Dim db As Database
  63.     Dim rs As Recordset
  64.     Dim sql As String
  65.     
  66.     On Error GoTo ReadRecordError
  67.     
  68.     If IsMissing(theDatabaseName) Then
  69.         If m_Database = "" Then Error ERR_DATABASENOTSPECIFIED
  70.     Else
  71.         m_Database = theDatabaseName
  72.     End If
  73.     
  74.     Set db = DBEngine.Workspaces(0).OpenDatabase(m_Database, False, True)
  75.     
  76.     If IsNumeric(whichAuthor) Then
  77.         Set rs = db.OpenRecordset("Authors", dbOpenTable)
  78.         rs.Index = "PrimaryKey"
  79.         rs.Seek "=", CLng(whichAuthor)
  80.         If rs.NoMatch Then Error ERR_CANTFINDRECORD
  81.     ElseIf VarType(whichAuthor) = vbString Then
  82.         sql = "SELECT * FROM Authors WHERE Author = '" & whichAuthor & "'"
  83.         Set rs = db.OpenRecordset(sql, dbOpenSnapshot)
  84.         If rs.RecordCount = 0 Then Error ERR_CANTFINDRECORD
  85.         rs.MoveLast
  86.         If rs.RecordCount <> 1 Then Error ERR_TOOMANYRECORDS
  87.     Else
  88.         Error ERR_WRONGVARTYPE
  89.     End If
  90.     m_ID = rs![Au_ID]
  91.     If Not IsNull(rs![author]) Then m_Name = rs![author]
  92.     If Not IsNull(rs![Year Born]) Then m_Born = rs![Year Born]
  93.  
  94.     rs.Close
  95.     db.Close
  96.     ReadAuthor = True
  97. Exit Function
  98. ReadRecordError:
  99.     LastError = Err.Number
  100.     ReadAuthor = False
  101. Exit Function
  102. End Function
  103. Public Function ReadTitles(Optional theDatabaseName As Variant) As Integer
  104.     Dim db As Database
  105.     Dim rs As Recordset
  106.     Dim sql As String
  107.     Dim ti As clsTitle
  108.     Dim i As Integer
  109.     
  110.     On Error GoTo ListTitlesError
  111.     
  112.     If m_ID < 1 Then Error ERR_OBJECTNOTINITIALIZED
  113.     
  114.     For i = m_Titles.Count To 1 Step -1
  115.         m_Titles.Remove i
  116.     Next i
  117.     
  118.     If IsMissing(theDatabaseName) Then
  119.         If m_Database = "" Then Error ERR_DATABASENOTSPECIFIED
  120.     Else
  121.         m_Database = theDatabaseName
  122.     End If
  123.     
  124.     
  125.     Set db = DBEngine.Workspaces(0).OpenDatabase(m_Database, False, True)
  126.     sql = "SELECT Titles.* FROM Authors "
  127.     sql = sql & " INNER JOIN (Titles INNER JOIN [Title Author]"
  128.     sql = sql & " ON Titles.ISBN = [Title Author].ISBN)"
  129.     sql = sql & " ON Authors.Au_ID = [Title Author].Au_ID"
  130.     sql = sql & " WHERE Authors.Au_ID =" & Str$(m_ID)
  131.     Set rs = db.OpenRecordset(sql, dbOpenSnapshot)
  132.     If rs.RecordCount <> 0 Then
  133.         rs.MoveFirst
  134.         Do
  135.             Set ti = New clsTitle
  136.             ti.Initialize rs, m_Database
  137.             m_Titles.Add ti
  138.             rs.MoveNext
  139.         Loop While Not rs.EOF
  140.     End If
  141.     ReadTitles = m_Titles.Count
  142. Exit Function
  143. ListTitlesError:
  144.     LastError = Err.Number
  145.     ErrorHandler vbExclamation
  146.     ReadTitles = 0
  147. Exit Function
  148.  
  149. End Function
  150.